package com.suke.money.Web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.money.suke.utils.PageModel;
import com.suke.money.model.BidInfo;
import com.suke.money.model.LoanInfo;
import com.suke.money.service.BidInfoService;
import com.suke.money.service.LoanInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: liush
 * @CreateTime: 2022-11-21  19:59
 * @Description: 产品控制层
 */
@Slf4j
@Controller
public class LoanController {

    @Reference(interfaceClass = LoanInfoService.class, version = "1.0.0", timeout = 20000)
    LoanInfoService loanInfoService;

    @Reference(interfaceClass = BidInfoService.class, version = "1.0.0", timeout = 20000)
    BidInfoService bidInfoService;

    @GetMapping("loan/loan")
    public String loan(@RequestParam(name = "ptype", required = true) Integer ptype, Long cunPage,
                       HttpServletRequest request, Model model) {

        //1.产品的展现

        /*Map<String, Object> parasMap = new HashMap<>();
        parasMap.put("ptype", ptype);
        parasMap.put("start", 0);
        parasMap.put("content", 9);
        List<LoanInfo> loanInfos = loanInfoService.queryLoanInfosByTypeAndNums(parasMap);
        model.addAttribute("loanInfos", loanInfos);*/

        /*
         * 共20条3页　当前为第2页 首页 上一页 下一页 尾页:
         * 不变：首页  尾页  总页数  总记录数   当前页  每页显示多少记录  ===》Map ===》分页模型
         * 变：上一页 下一页
         *
         * 分析  抽象 封装  继承 多态
         * */
        //1.创建分页模型
        PageModel pageModel = (PageModel) request.getSession().getAttribute("pageModel");
        if (pageModel == null) {
            pageModel = new PageModel(9);
            request.getSession().setAttribute("pageModel", pageModel);
        }
        //1.1查询总记录数
        Long totalCount = loanInfoService.queryLoanInfoNumsByType(ptype);
        //1.2设置总记录数
        pageModel.setTotalCount(totalCount);
        //1.3没有当前页 或 当前页<1
        if (null == cunPage || cunPage <= 0) {
            cunPage = pageModel.getFirstPage().longValue();
        }
        //1.4当前页>尾页
        if (cunPage >= pageModel.getLastPage()) {
            cunPage = pageModel.getLastPage();
        }
        //1.5设置当前页
        pageModel.setCunPage(cunPage);
        //2.根据类型和分页模型 查询 产品信息
        List<LoanInfo> loanInfos = loanInfoService.queryLoanInfosByTypeAndPageModel(ptype, pageModel);
        model.addAttribute("loanInfos", loanInfos);
        model.addAttribute("ptype", ptype);

        return "loan.html";
    }

    @GetMapping("/loan/loanInfo")
    public String loanINfo(@RequestParam(name = "loanId", required = true) Integer loanId,
                           Long cunPage, Model model) {
        //查询产品信息
        LoanInfo loanInfo = loanInfoService.queryLoanInfoById(loanId);
        model.addAttribute("loanInfo", loanInfo);

        //查询该产品投资记录
        //1.创建分页模型
        PageModel pageModel = (PageModel) model.getAttribute("pageModel");
        if (pageModel == null) {
            pageModel = new PageModel(8);
            //2.查询总记录数
            Long totalCount = bidInfoService.queryBidInfoNumsByLoanId(loanId);
            pageModel.setTotalCount(totalCount);
            //3.1没有当前页 或 当前页<1
            if (null == cunPage || cunPage <= 0) {
                cunPage = pageModel.getFirstPage().longValue();
            }
            //3.2当前页>尾页
            if (cunPage >= pageModel.getLastPage()) {
                cunPage = pageModel.getLastPage();
            }
            //3.3设置当前页
            pageModel.setCunPage(cunPage);
            model.addAttribute("pageModel", pageModel);
        }

        List<BidInfo> bidInfos = bidInfoService.queryBidInfosByLoanIdAndPageModel(loanId, pageModel);
        model.addAttribute("bidInfos", bidInfos);

        return "loanInfo.html";
    }
}
